package com.ghost.alibabaghost.web.controller.sys;

import com.ghost.alibabaghost.api.service.sys.SysMenusService;
import com.ghost.alibabaghost.api.model.sys.SysMenusModel;
import com.ghost.alibabaghost.result.SingleResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author yaolisheng
 * @version 1.0
 * @date 2022/1/15 08:34
 */
@RestController
public class SysMenusController {

    private static final Logger logger = LoggerFactory.getLogger(SysMenusModel.class);

    @Autowired
    private SysMenusService sysMenusService;

    /**
     * 获取菜单列表
     *
     * @return
     */
    @RequestMapping("getMenusList")
    public SingleResult<List<SysMenusModel>> getMenusList() {
        logger.info("getMenusList(获取菜单列表)");
        List<SysMenusModel> nodeTree = sysMenusService.getNodeTree();
        return SingleResult.buildSuccess(nodeTree);
    }

    /**
     * 查询节点信息
     *
     * @return
     */
    @RequestMapping("queryByNodeId")
    public SingleResult<SysMenusModel> queryByNodeId(@RequestParam("nodeId") String nodeId) {
        logger.info("queryByNodeId(查询节点信息)-->param:nodeId=" + nodeId);
        if (nodeId==null) return SingleResult.buildFailure();
        try {
            SysMenusModel sysMenusModel = sysMenusService.queryByNodeId(nodeId);
            if (sysMenusModel != null) return SingleResult.buildSuccess(sysMenusModel);
            return SingleResult.buildFailure();
        } catch (Exception e) {
            logger.error("queryByNodeId(查询节点信息)-->",e);
            return SingleResult.buildFailure();
        }
    }

    /**
     * 更新菜单
     *
     * @return
     */
    @RequestMapping("updateMenu")
    public SingleResult<String> updateMenu(@RequestBody SysMenusModel sysMenusModel) {
        logger.info("updateMenu()更新菜单--->param:sysMenusModel="+sysMenusModel);
        try {
            sysMenusService.updateMenu(sysMenusModel);
            return SingleResult.buildSuccess("更新成功");
        } catch (Exception e) {
            logger.error("updateMenu()更新菜单",e);
            return SingleResult.buildFailure();
        }
    }

    /**
     * 新增菜单
     *
     * @return
     */
    @RequestMapping("insertMenu")
    public SingleResult<String> insertMenu(@RequestBody SysMenusModel sysMenusModel) {
        logger.info("insertMenu(新增菜单)-->params:sysMenusModel="+sysMenusModel);
        try {
            sysMenusService.insertMenu(sysMenusModel);
            return SingleResult.buildSuccess("成功");
        } catch (Exception e) {
            logger.error("nsertMenu(新增菜单)--->",e);
            return SingleResult.buildFailure();
        }
    }

}
